package com.xxpt.common.auth;

import com.xxpt.common.constant.SysConstant;
import com.xxpt.common.enums.UserTypeEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 授权信息
 * @Author masl - 2020/4/8 10:13
 */
@Data
public class AuthProperties implements Serializable {

    //用户Id
    private Integer uid;

    //用户类别（1：学员 2：单位 3：子帐号）
    private Integer uType;

    private String token;

    public AuthProperties() {
    }

    public AuthProperties(Integer uid, Integer uType) {
        this.uid = uid;
        this.uType = uType;
    }

    /**
     * 去除多余字段、避免 token 过长
     * @return
     */
    public String toJSONString() {
        return "{" +
                "\"uid\":" + uid +
                ", \"uType\":" + uType +
                "}";
    }

    /**
     * 获取登录key
     * @return
     */
    public String getSessionKey() {
        UserTypeEnum userTypeEnum = UserTypeEnum.getUserTypeEnum(uType);
        switch (userTypeEnum) {
            case PERSON:
                // 学员key: session前缀+userId
                return SysConstant.SESSION_STUDENT_KEY_PREFIX + uid;
            case DEPARTMENT:
                return SysConstant.SESSION_DEPARTMENT_KEY_PREFIX + uid;
            case DEPARTMENT_CHILD:
                return SysConstant.SESSION_DEPARTMENT_CHILD_KEY_PREFIX + uid;
        }
        return "";
    }
}
